package com.gyj.user.controller;

import com.gyj.common.CommonResult;
import com.gyj.user.model.User;
import com.gyj.user.service.HashRedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
/**
 * Created by Gao on 2018/9/30.
 */
@Api(tags = "Redis中hash数据结构")
@RestController
@RequestMapping(value = "/hashRedis", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class HashRedisController {

    @Autowired
    private HashRedisService hashRedisService;

    @ApiOperation(value = "根据id获取user对象，没有从mysql中取, 然后放入缓存中")
    @GetMapping("getUserByIdHash1")
    public User getUserByIdHash(@RequestParam("id") String id) {

        User userByIdHash = hashRedisService.getUserByIdHash(id);
        return userByIdHash;
    }

    @ApiOperation(value = "根据ids获取userList，没有从mysql中取, 然后放入缓存中")
    @GetMapping("getUserListByIdsHash")
    public CommonResult<List<User>> getUserByIdHash(@RequestParam("id") List<Integer> ids) {
        CommonResult<List<User>> result = null;
        try {
            result = hashRedisService.getUserListByIdsHash(ids);
        } catch (Exception e) {
            e.printStackTrace();
            result = new CommonResult<>(e);
            result.setSuccess(false);
            result.setMessage(e.getMessage());
            return result;
        }
        return result;
    }
}
